package com.sinosoft.perfect.system._user.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TDisplay;
import com.sinosoft.perfect.system._.po.TTheme;
import com.sinosoft.perfect.system._.po.TUnit;
import com.sinosoft.perfect.system._.po.TUser;
import com.sinosoft.perfect.system._.po.TUserImg;
import com.sinosoft.perfect.system._.po.TUserInformation;
import com.sinosoft.perfect.system._.po.TUserRolebusinessRelation;
import com.sinosoft.perfect.system._.po.TUserUnitRelation;
import com.sinosoft.perfect.system._.util.FileUtil;
import com.sinosoft.perfect.system._.util.ImageUtil;
import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system._display.service.IDisplayService;
import com.sinosoft.perfect.system._theme.service.IThemeService;
import com.sinosoft.perfect.system._unit.formBean.UnitForm;
import com.sinosoft.perfect.system._unit.service.IUnitService;
import com.sinosoft.perfect.system._user.dataFormat.IUserGridDataFormat;
import com.sinosoft.perfect.system._user.dataFormat.IUserTreeDataFormat;
import com.sinosoft.perfect.system._user.formBean.UserForm;
import com.sinosoft.perfect.system._user.service.IUserService;
import com.sinosoft.perfect.system._user.vo.UserStaticVO;
import com.sinosoft.perfect.system._user.vo.UserUnitVO;

@SuppressWarnings("serial")
public class UserAction extends BaseAction {

	private IUserService userService;

	private IUnitService unitService;

	private IThemeService themeService;

	private IDisplayService displayService;

	private IUserGridDataFormat userGridDataFormat;

	private IUserTreeDataFormat userTreeDataFormat;

	private TUser tuser;

	private UserForm userForm;

	private TUserInformation tuserinfo;

	private TUserImg tuserimg;

	private TUserUnitRelation tuserunitrelation;

	private TUserRolebusinessRelation tuserrolebusiness;

	private UserStaticVO userStatic;

	private String userMainId;

	private String[] userMainIds;

	private String unitId;

	private String roleBusinessId;
	
	private String roleSystemId;//fys for the method of UserAction.userGridFromRoleSystem

	private File userImg;

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IUnitService getUnitService() {
		return unitService;
	}

	public void setUnitService(IUnitService unitService) {
		this.unitService = unitService;
	}

	public IThemeService getThemeService() {
		return themeService;
	}

	public void setThemeService(IThemeService themeService) {
		this.themeService = themeService;
	}

	public IDisplayService getDisplayService() {
		return displayService;
	}

	public void setDisplayService(IDisplayService displayService) {
		this.displayService = displayService;
	}

	public IUserGridDataFormat getUserGridDataFormat() {
		return userGridDataFormat;
	}

	public void setUserGridDataFormat(IUserGridDataFormat userGridDataFormat) {
		this.userGridDataFormat = userGridDataFormat;
	}

	public IUserTreeDataFormat getUserTreeDataFormat() {
		return userTreeDataFormat;
	}

	public void setUserTreeDataFormat(IUserTreeDataFormat userTreeDataFormat) {
		this.userTreeDataFormat = userTreeDataFormat;
	}

	public TUser getTuser() {
		return tuser;
	}

	public void setTuser(TUser tuser) {
		this.tuser = tuser;
	}

	public UserForm getUserForm() {
		return userForm;
	}

	public void setUserForm(UserForm userForm) {
		this.userForm = userForm;
	}

	public TUserInformation getTuserinfo() {
		return tuserinfo;
	}

	public void setTuserinfo(TUserInformation tuserinfo) {
		this.tuserinfo = tuserinfo;
	}

	public TUserImg getTuserimg() {
		return tuserimg;
	}

	public void setTuserimg(TUserImg tuserimg) {
		this.tuserimg = tuserimg;
	}

	public TUserUnitRelation getTuserunitrelation() {
		return tuserunitrelation;
	}

	public void setTuserunitrelation(TUserUnitRelation tuserunitrelation) {
		this.tuserunitrelation = tuserunitrelation;
	}

	public TUserRolebusinessRelation getTuserrolebusiness() {
		return tuserrolebusiness;
	}

	public void setTuserrolebusiness(TUserRolebusinessRelation tuserrolebusiness) {
		this.tuserrolebusiness = tuserrolebusiness;
	}

	public UserStaticVO getUserStatic() {
		return userStatic;
	}

	public void setUserStatic(UserStaticVO userStatic) {
		this.userStatic = userStatic;
	}

	public String getUserMainId() {
		return userMainId;
	}

	public void setUserMainId(String userMainId) {
		this.userMainId = userMainId;
	}

	public String[] getUserMainIds() {
		return userMainIds;
	}

	public void setUserMainIds(String[] userMainIds) {
		this.userMainIds = userMainIds;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public String getRoleBusinessId() {
		return roleBusinessId;
	}

	public void setRoleBusinessId(String roleBusinessId) {
		this.roleBusinessId = roleBusinessId;
	}

	public String getRoleSystemId() {
		return roleSystemId;
	}

	public void setRoleSystemId(String roleSystemId) {
		this.roleSystemId = roleSystemId;
	}

	public File getUserImg() {
		return userImg;
	}

	public void setUserImg(File userImg) {
		this.userImg = userImg;
	}

	/** ———————————————————————————————————————————————————————————— **/
	/** ———————————————————————————— User —————————————————————————— **/
	/** ———————————————————————————————————————————————————————————— **/

	public String userTree() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			TUnit tunit = new TUnit();
			tunit.setStatus("1");
			tunit.setUnitLevel(1);
			List<UnitForm> units = unitService.selectTUnit(tunit);
			resultXml = userTreeDataFormat.dataFormatUserTree(units);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String userTreeChild() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String unitTreeId = request.getParameter("id"); // 动态加载树时，展开的节点id
		String resultXml = null;
		try {
			List<UserUnitVO> userunits = userService.selectTUserAndTUnit(unitTreeId);
			resultXml = userTreeDataFormat.dataFormatUserTree(userunits, unitTreeId);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String unitTree() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			TUnit tunit = new TUnit();
			tunit.setStatus("1");
			List<UnitForm> units = unitService.selectTUnit(tunit);
			resultXml = userTreeDataFormat.dataFormatUnitTree(units);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String userGrid() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TUser> users = userService.selectTUser(tuser);
			resultXml = userGridDataFormat.dataFormatUserGrid(users);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String userGridFromUnit() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<UserForm> userForms = userService.selectTUserFromUnit(userForm);
			resultXml = userGridDataFormat.dataFormatUserFromUnitGrid(userForms);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String userGridFromRoleBusiness() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<UserForm> userForms = userService.selectTUserFromRoleBusiness(userForm);
			resultXml = userGridDataFormat.dataFormatUserFromRoleBusinessGrid(userForms);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}
	
	/**
	 * author fys (11-07-21) 用来显示系统角色下的用户grid
	 * @return xml
	 * @throws IOException
	 */
	public String userGridFromRoleSystem() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<UserForm> userForms = userService.selectTUserFromRoleSystem(roleSystemId);
			resultXml = userGridDataFormat.dataFormatUserFromRoleSystemGrid(userForms);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String userGridDetach() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TUser> users = userService.selectTUserDetached(tuser);
			resultXml = userGridDataFormat.dataFormatUserDetachGrid(users);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String userGridStatic() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<UserStaticVO> userStatics = userService.selectTUserStatic(userStatic);
			resultXml = userGridDataFormat.dataFormatUserStaticGrid(userStatics);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String userQueryForEdit() {
		try {
			TUser unit = new TUser();
			unit.setUserMainId(userMainId);
			List<TUser> users = userService.selectTUser(unit);
			if (users != null && users.size() != 0) {
				request.setAttribute("userEdit", users.get(0));
			}

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "userEdit";
	}

	public String userEdit() throws Exception {
		try {
			userService.updateTUserByPK(userForm);
			this.userMainId = userForm.getUserMainId();// 更新后，通过userMainId查询
		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return userQueryForEdit();
	}

	public String userDelUnit() throws Exception {
		try {
			userService.deleteTUserUnit(userMainId, unitId);

		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return null;
	}

	public String userDelAll() throws Exception {
		try {
			userService.deleteTUserAll(userMainId);

		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return null;
	}

	/** ———————————————————————————————————————————————————————————— **/
	/** —————————————————————————User Info—————————————————————————— **/
	/** ———————————————————————————————————————————————————————————— **/
	public String userInfoQuery() {
		try {
			List<TUserInformation> userInfos = userService.selectTUserInfo(tuserinfo);
			if (userInfos != null && userInfos.size() != 0)
				request.setAttribute("userInfos", userInfos);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "userInfoList";
	}

	public String userInfoQueryForEdit() {
		try {
			TUserInformation userInfo = new TUserInformation();
			userInfo.setUserMainId(userMainId);
			List<TUserInformation> userInfos = userService.selectTUserInfo(userInfo);
			if (userInfos != null && userInfos.size() != 0)
				request.setAttribute("userInfoEdit", userInfos.get(0));

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "userInfoEdit";
	}

	public String userInfoQueryForEditPerson() {
		try {
			TUserInformation userInfo = new TUserInformation();
			userInfo.setUserMainId(userMainId);
			List<TUserInformation> userInfos = userService.selectTUserInfo(userInfo);
			if (userInfos != null && userInfos.size() != 0)
				request.setAttribute("userInfoEdit", userInfos.get(0));

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "userInfoEditPerson";
	}

	public String userInfoEdit() {
		try {
			userService.updateTUserInfoByPK(tuserinfo);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		this.userMainId = tuserinfo.getUserMainId();// 更新后，通过userMainId查询
		return userInfoQueryForEdit();
	}

	public String userInfoEditClear() {
		try {
			TUserInformation userInfo = new TUserInformation();
			userInfo.setId(tuserinfo.getId());
			userInfo.setUserMainId(tuserinfo.getUserMainId());
			userService.updateTUserInfoByPK(userInfo);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		this.userMainId = tuserinfo.getUserMainId();// 更新后，通过userMainId查询
		return userInfoQueryForEdit();
	}

	public String userInfoEditClearPerson() {
		try {
			TUserInformation userInfo = new TUserInformation();
			userInfo.setId(tuserinfo.getId());
			userInfo.setUserMainId(tuserinfo.getUserMainId());
			userService.updateTUserInfoByPK(userInfo);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		this.userMainId = tuserinfo.getUserMainId();// 更新后，通过userMainId查询
		return userInfoQueryForEditPerson();
	}

	/** ———————————————————————————————————————————————————————————— **/
	/** —————————————————————————User Img——————————————————————————— **/
	/** ———————————————————————————————————————————————————————————— **/
	public String userImgQueryForEdit() {
		try {
			TUserImg userImg = new TUserImg();
			userImg.setUserMainId(userMainId);
			List<TUserImg> userImgs = userService.selectTUserImg(userImg);

			if (userImgs != null && userImgs.size() != 0) {
				if (userImgs.get(0).getImg() != null && userImgs.get(0).getImg().length != 0) {
					String imgDir = "" + session.get("tempFolder");
					String imgName = Producer.generateIdOfTen() + ".jpg";
					FileUtil.getFileFromBytes(userImgs.get(0).getImg(), imgDir + "/" + imgName);
					request.setAttribute("imgName", imgName);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "userImgEdit";
	}

	public String userImgUpload() {
		try {
			if (userImg != null && userImg.length() > 1024000) {
				request.setAttribute("filelength", "big");
				return userImgQueryForEdit();
			}

			String imgPath = "" + session.get("tempFolder");
			String imgName = Producer.generateIdOfTen() + ".jpg";
			File saveFile = new File(imgPath + "/" + imgName);
			FileUtils.copyFile(userImg, saveFile);

			request.setAttribute("uploadImgName", imgName);

		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return userImgQueryForEdit();
	}

	public String userImgEdit() {
		try {
			String imgPath = "" + session.get("tempFolder");
			String uploadImgName = request.getParameter("imgName");
			File fileSource = new File(imgPath + "/" + uploadImgName);

			String imgName = Producer.generateIdOfTen() + ".jpg";
			File fileTarget = new File(imgPath + "/" + imgName);

			int imageWidth = Integer.parseInt(request.getParameter("txt_width"));
			int imageHeight = Integer.parseInt(request.getParameter("txt_height"));
			int cutTop = Integer.parseInt(request.getParameter("txt_top"));
			int cutLeft = Integer.parseInt(request.getParameter("txt_left"));
			int dropWidth = Integer.parseInt(request.getParameter("txt_DropWidth"));
			int dropHeight = Integer.parseInt(request.getParameter("txt_DropHeight"));

			ImageUtil.doImg(fileSource, fileTarget, imageWidth, imageHeight, ImageUtil.createRec(cutLeft, cutTop, dropWidth, dropHeight));

			byte[] img = FileUtil.getBytesFromFile(fileTarget);
			TUserImg userImg = new TUserImg();
			userImg.setUserMainId(userMainId);
			userImg.setImg(img);
			userService.updateTUserImg(userImg);

			request.setAttribute("imgName", imgName);

		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		request.setAttribute("changeImg", "win");
		return userImgQueryForEdit();
	}

	public String userImgEditClear() {
		try {
			TUserImg userImg = new TUserImg();
			userImg.setUserMainId(userMainId);
			userService.updateTUserImg(userImg);

		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		request.setAttribute("changeImg", "win");
		return userImgQueryForEdit();
	}

	/** ———————————————————————————————————————————————————————————— **/
	/** —————————————————————————User Theme————————————————————————— **/
	/** ———————————————————————————————————————————————————————————— **/
	public String userThemeUpdate() throws Exception {
		try {
			userService.updateTUserByPK(userForm);
			this.userMainId = userForm.getUserMainId();// 更新后，通过userMainId查询
		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return userThemeQuery();
	}

	public String userThemeQuery() {
		try {
			TUser user = new TUser();
			user.setUserMainId(userMainId);
			List<TUser> users = userService.selectTUser(user);
			String themeId = null;
			if (users != null && users.size() != 0) {
				themeId = users.get(0).getThemeId();
				request.setAttribute("themeId", themeId);
			}

			TTheme theme = new TTheme();
			theme.setStatus("1"); // 只查有效的主题
			List<TTheme> themes = themeService.selectByTTheme(theme);
			if (themes != null && themes.size() != 0)
				request.setAttribute("themes", themes);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "userThemeList";
	}

	/** ———————————————————————————————————————————————————————————— **/
	/** —————————————————————————User Display——————————————————————— **/
	/** ———————————————————————————————————————————————————————————— **/
	public String userDisplayUpdate() throws Exception {
		try {
			userService.updateTUserByPK(userForm);
			this.userMainId = userForm.getUserMainId();// 更新后，通过userMainId查询
		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return userDisplayQuery();
	}

	public String userDisplayQuery() {
		try {
			TUser user = new TUser();
			user.setUserMainId(userMainId);
			List<TUser> users = userService.selectTUser(user);
			String frameModelId = null;
			if (users != null && users.size() != 0) {
				frameModelId = users.get(0).getFrameModelId();
				request.setAttribute("frameModelId", frameModelId);
			}

			TDisplay display = new TDisplay();
			display.setStatus("1"); // 只查有效的模板
			List<TDisplay> displays = displayService.selectByTDisplay(display);
			if (displays != null && displays.size() != 0)
				request.setAttribute("displays", displays);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "userDisplayList";
	}
}
